#include "YXResult.h"

ResultUtils YXResultUtils;

static YXResult *successWithMsg(char *msg, yxObject *obj) {
    yxObject *result = cJSON_CreateObject();
    cJSON_AddNumberToObject(result, "code", 1);
    if (msg != NULL) {
        cJSON_AddStringToObject(result, "msg", msg);
    } else {
        cJSON_AddStringToObject(result, "msg", "success");
    }
    if (obj != NULL) {
        cJSON_AddItemToObject(result, "data", obj);
    }
    return result;
}

static YXResult *success(yxObject *obj) {
    yxObject* result = cJSON_CreateObject();
    cJSON_AddNumberToObject(result, "code", 1);
    cJSON_AddStringToObject(result, "msg", "success");
    if (obj != NULL) {
        cJSON_AddItemToObject(result, "data", obj);
    }
    return result;
}

static YXResult *error(char *msg) {
    yxObject *result = cJSON_CreateObject();
    cJSON_AddNumberToObject(result, "code", 0);
    if (msg != NULL) {
        cJSON_AddStringToObject(result, "msg", msg);
    } else {
        cJSON_AddStringToObject(result, "msg", "success");
    }
    return result;
}


__attribute__((constructor(101))) void YXResultUtilsInit(void) {
    YXResultUtils.successWithMsg = successWithMsg;
    YXResultUtils.success = success;
    YXResultUtils.error   = error;
}
